package com.lingnan.config;

import com.lingnan.config.NotificationWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration("WebSocketConfig")
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private NotificationWebSocketHandler webSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/ws/notification")
                .setAllowedOrigins("*") // 允许所有来源，或者指定具体域名
                .withSockJS(); // 如果需要SockJS支持

        // 也可以不使用SockJS，直接支持原生WebSocket
        registry.addHandler(webSocketHandler, "/ws/notification")
                .setAllowedOrigins("*");
    }
}